package com.heihei.util.newimgmerge;

import com.freewayso.image.combiner.ImageCombiner;
import com.freewayso.image.combiner.enums.OutputFormat;
import com.freewayso.image.combiner.enums.ZoomMode;
import com.heihei.util.newimgmerge.dto.ImageParamsDTO;
import com.heihei.util.newimgmerge.dto.ImgDetailInfoDTO;
import com.heihei.util.newimgmerge.dto.LineImageDTO;
import com.heihei.util.FileUtilF;
import com.heihei.util.ImgUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class NewImagePreviewUtil {
    /**
     * 测试用
     *
     * @author dhf
     * 2022年08月19日 23:03:18
     */
    public static void main(String[] args) throws Exception {
        //图片列表
        List<BufferedImage> imageList = new ArrayList<>();
        File originalDir = new File("C:\\Users\\12851\\Desktop\\124");
        FileUtilF.handleFiles(originalDir, file -> {
            if(FileUtilF.isImg(file)) {
                imageList.add(ImgUtil.loadImageLocal(file));
            }
        });
        Color bgColor = new Color(204, 255, 255);
        File targetFile = new File("C:\\Users\\12851\\Desktop\\target_test.jpg");
        mergeImgFile(1540, 2,
                2, 2, 3, imageList, targetFile, bgColor);
    }

    /**
     * 合并图片，用这个
     * @param containerWidth 容器宽度
     * @param horizontalInterval 横向图片间隔
     * @param verticalInterval 纵向图片间隔
     * @param horizontalImgLineCount 横版图片每行个数
     * @param verticalImgLineCount 纵版图片每行个数
     * @param originalImageList 源图片list
     * @param targetFile 目标jpg图片文件
     *
     * @param bgColor
     * @author dhf
     * 2022年08月19日 22:38:20
     */
    private static void mergeImgFile(int containerWidth, int horizontalInterval, int verticalInterval,
                                     int horizontalImgLineCount, int verticalImgLineCount,
                                     List<BufferedImage> originalImageList, File targetFile, Color bgColor) throws Exception {
        /**
         参数最好需要满足：  以下两个运算，需要是整数
         (containerWidth - (horizontalInterval * (horizontalImgLineCount + 1))) / horizontalImgLineCount
         (containerWidth - (verticalInterval * (verticalImgLineCount + 1))) / verticalImgLineCount
         */
        ImageParamsDTO paramsDTO = new ImageParamsDTO(containerWidth, horizontalInterval, verticalInterval, horizontalImgLineCount, verticalImgLineCount, originalImageList, targetFile, bgColor);
        generateImgFile(paramsDTO);
    }

    public static void generateImgFile(ImageParamsDTO paramsDTO) throws Exception {
        List<LineImageDTO> allLineList = paramsDTO.getAllLineInfoList();
        ImageCombiner combiner = null;
        if(paramsDTO.getBgColor() == null) {
            combiner = new ImageCombiner(paramsDTO.getContainerWidth(), paramsDTO.getContainerHeight(), OutputFormat.JPG);
        } else {
            combiner = new ImageCombiner(paramsDTO.getContainerWidth(), paramsDTO.getContainerHeight(), paramsDTO.getBgColor(), OutputFormat.JPG);
        }

        int x = paramsDTO.getHorizontalInterval();
        int y = paramsDTO.getVerticalInterval();
        for (int i = 0; i < allLineList.size(); i++) {
//        for (LineImageDTO lineDto : allLineList) {
            LineImageDTO lineDto = allLineList.get(i);
            //最后一行，把图片放中间
            if(i == allLineList.size() - 1) {
                //求最后一行的有效宽度
                int validWidth = 0;
                for (ImgDetailInfoDTO img : lineDto.getLineImageList()) {
                    validWidth += img.getStandardWidth();
                }
                validWidth += (lineDto.getLineImageList().size() - 1) * paramsDTO.getHorizontalInterval();
                x = (paramsDTO.getContainerWidth() - validWidth) / 2;
            }
            for (ImgDetailInfoDTO img : lineDto.getLineImageList()) {
                combiner.addImageElement(img.getBufferedImage(), x, y, img.getStandardWidth(), img.getStandardHeight(), ZoomMode.WidthHeight);
                x += img.getStandardWidth() + paramsDTO.getHorizontalInterval();
            }
            y += lineDto.getLineHeight() + paramsDTO.getVerticalInterval();
            x = paramsDTO.getHorizontalInterval();
        }
        combiner.combine();
        combiner.save(paramsDTO.getTargetFile().getAbsolutePath());
    }
}
